package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.WeChatMqApi;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000433Bo;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TransferProcessHistoryDaoImp;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.dto.TransferProcessHistoryDto;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.CheckConfirmActionForm;
import com.tianqu.tms.www.transfer.viewhelper.CheckConfirmViewHelper;

public class StatusControlInputResultController extends TmsController {
	private Logger logger = Logger.getLogger(StatusControlInputResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			CheckConfirmActionForm form = (CheckConfirmActionForm) uc.getInputForm();
			//
			TmsUserProfile profile = uc.getUserProfile();
			TransferAcceptedInfoDto dto = new TransferAcceptedInfoDto();
			Integer status = Integer.parseInt(form.getStatus());
			if(status==13) {
				dto.setStatus(11);
			} else {
				dto.setStatus(status);
			}
			dto.setSerial(form.getSerial());
			dto.setInsertUserid(profile.getUniqueId());
			TransferAcceptedInfo.getInstance().updateAcceptInfo002(dto);
			//
			// 插入 过户流程操作记录表（tms_transfer_process_history）
			TransferProcessHistoryDto transferProcessHistoryDto = new TransferProcessHistoryDto();
			if("10".equals(form.getStatus())) transferProcessHistoryDto.setBusinessType(10);
			else if("11".equals(form.getStatus())) {
				transferProcessHistoryDto.setBusinessType(20);
				transferProcessHistoryDto.setReason(form.getRemarks());
			}
			else if("12".equals(form.getStatus())) transferProcessHistoryDto.setBusinessType(11);
			else if("13".equals(form.getStatus())) {
				transferProcessHistoryDto.setBusinessType(22);
				transferProcessHistoryDto.setReason(form.getRemarks());
			}
			transferProcessHistoryDto.setInsertUserId(profile.getUniqueId());
			transferProcessHistoryDto.setSerial(form.getSerial());
			transferProcessHistoryDto.setReason(form.getRemarks());
			TransferProcessHistoryDaoImp transferProcessHistoryDao = (TransferProcessHistoryDaoImp) TransferProcessHistoryDaoImp.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
			transferProcessHistoryDao.insertHisInfoBySerial(transferProcessHistoryDto);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
			
			//API 433
			TransferDataSyn000433Bo bo = new TransferDataSyn000433Bo();
			bo.setSerial(form.getSerial());
			bo.setRemark(form.getRemarks());
			bo.setStatus(Integer.parseInt(form.getStatus()));
			bo.setMarketNo(TmsConstants.MARKET_NO);
			WeChatMqApi.getInstance().call(bo);
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
}
